###############################################################################   
#### Replication Materials                                                 #### 
#### Taegyoon Kim, 2022. Violent Political Rhetoric on Twitter.            ####
#### Political Science Research and Methods                                ####
###############################################################################  


###############################################################################
################################### Set Up ####################################
###############################################################################

# packages -------------------------

lapply(c('readr', 'gridExtra', 'xtable', 'hexbin', 'ggplot2', 
         'ggthemes',  'dplyr', 'tidyr', 'magrittr'), 
       require, 
       character.only = TRUE)

# path ------------------------- 

path_data <- '/kim_psrm_replication/data/' 
path_output <- '/kim_psrm_replication/output/' 


###############################################################################
######################### Generate Figures 5a and 5d ##########################
###############################################################################

# figure 5b -------------------------

df_ideology <- read_csv(paste0(path_data, 'df_ideology.csv'), 
                                 col_types = cols(X1 = col_skip())) 

df_ideology$type <- ifelse(df_ideology$type == 'political', 'Non-violent', 'Violent')
names(df_ideology)[names(df_ideology) == 'type'] <- 'Type'

df_ideology <- df_ideology[order(df_ideology$week), ] 
df_ideology$week <- as.factor(df_ideology$week)
df_ideology$week <- factor(df_ideology$week, 
                           levels = c('week16', 'week15', 'week14', 'week13', 
                                      'week12', 'week11', 'week10', 'week9', 
                                      'week8', 'week7', 'week6', 'week5', 
                                      'week4', 'week3', 'week2', 'week1'))
df_ideology$Type <- factor(df_ideology$Type, 
                           levels = c('Violent','Non-violent'))

ggplot(df_ideology, aes(x = week, y = ideology_final, color = Type)) + 
  theme_classic() +
  geom_boxplot(outlier.shape = NA) + 
  scale_color_manual(values = c('red','skyblue')) +
  scale_x_discrete(labels = rev(c('Sep 23', 'Sep 30', 'Oct 7', 'Oct 14', 
                                  'Oct 21', 'Oct 28', 'Nov 4', 'Nov 11',
                                  'Nov 18', 'Nov 25', 'Dec 2', 'Dec 9', 
                                  'Dec 16', 'Dec 23', 'Dec 30', 'Jan 6'))) +
  labs(x = 'First Date of Week\n', y = '\n Estimated Ideology Score') +
  theme(legend.text = element_text(size = 17),
        legend.title = element_text(size = 17),
        legend.position = 'top',
        axis.text.x = element_text(size = 17, angle = 0, vjust = 0, hjust = 0.5),
        axis.text.y = element_text(size = 17),
        axis.title = element_text(size = 17)) + 
  coord_flip()

ggsave(paste0(path_output, 'fig5b.pdf'),
       dpi = 600,
       width = 6,
       height = 10,
       units = 'in')

# figure 5d -------------------------

df_extremity <- read_csv(paste0(path_data, 'df_extremity.csv'), 
                        col_types = cols(X1 = col_skip())) 

df_extremity$type <- ifelse(df_extremity$type == 'political', 'Non-violent', 'Violent')
names(df_extremity)[names(df_extremity) == 'type'] <- 'Type'

df_extremity <- df_extremity[order(df_extremity$week), ] 
df_extremity$week <- as.factor(df_extremity$week)
df_extremity$week <- factor(df_extremity$week, levels = c('week16', 'week15', 'week14', 'week13', 
                                                        'week12', 'week11', 'week10', 'week9', 
                                                        'week8', 'week7', 'week6', 'week5', 
                                                        'week4', 'week3', 'week2', 'week1'))
df_extremity$Type <- factor(df_extremity$Type, levels = c('Violent','Non-violent'))

ggplot(df_extremity, aes(x = week, y = ideology_final_abs, color = Type)) + 
  theme_classic() +
  geom_boxplot(outlier.shape = NA) + 
  scale_color_manual(values=c('red','skyblue')) +
  scale_x_discrete(labels=rev(c('Sep 23', 'Sep 30', 'Oct 7', 'Oct 14', 
                                'Oct 21', 'Oct 28', 'Nov 4', 'Nov 11', 
                                'Nov 18', 'Nov 25', 'Dec 2', 'Dec 9', 
                                'Dec 16', 'Dec 23', 'Dec 30', 'Jan 6'))) +
  labs(x = 'First Date of Week\n', y = '\nEstimated Ideological Extremity Score') +
  theme(legend.text = element_text(size = 17),
        legend.title = element_text(size = 17),
        legend.position='top',
        axis.text.x = element_text(size = 17, angle = 0, vjust = 0, hjust = 0.5),
        axis.text.y = element_text(size = 17),
        axis.title = element_text(size = 17))  + 
  coord_flip()

ggsave(paste0(path_output, 'fig5d.pdf'),
       dpi = 600,
       width = 6,
       height = 10,
       units = 'in')


###############################################################################
######################### Generate Figures 6a and 6b ##########################
###############################################################################

# figure 6a -------------------------

df_homophily <- read_csv(paste0(path_data, 'df_homophily.csv'), 
                         col_types = cols(X1 = col_skip())) 

df_homophily_violent <- df_homophily[which(df_homophily$type=='violent'), ]

ggplot(df_homophily_violent, 
       aes(x = tweeter_ideology_final, 
           y = retweeter_ideology_final)) +
  geom_hex(bins = 80) + 
  theme_bw() +
  scale_fill_continuous('Count', type = 'gradient') +
  labs(x = 'Tweeter Estimated Ideology Score', y = 'Retweeter Estimated Ideology Score') + 
  theme(legend.text = element_text(size = 17),
        legend.title = element_text(size = 17),
        axis.text.x = element_text(size = 17),
        axis.text.y = element_text(size = 17),
        axis.title = element_text(size = 17))

ggsave(paste0(path_output, 'fig6a.pdf'),
       dpi = 600,
       width = 5,
       height = 5,
       units = 'in')

cor(df_homophily_violent$tweeter_ideology_final, 
    df_homophily_violent$retweeter_ideology_final, 
    use='complete.obs')

# figure 6b -------------------------

df_homophily_political <- df_homophily[which(df_homophily$type=='political'), ]

ggplot(df_homophily_political, 
       aes(x = tweeter_ideology_final, 
           y = retweeter_ideology_final)) +
  geom_hex(bins = 80) + 
  theme_bw() +
  scale_fill_continuous('Count', type = 'gradient') +
  labs(x = 'Tweeter Estimated Ideology Score', y = 'Retweeter Estimated Ideology Score') + 
  theme(legend.text = element_text(size = 17),
        legend.title = element_text(size = 17),
        axis.text.x = element_text(size = 17),
        axis.text.y = element_text(size = 17),
        axis.title = element_text(size = 17))

ggsave(paste0(path_output, 'fig6b.pdf'),
       dpi = 600,
       width = 5,
       height = 5,
       units = 'in')

cor(df_homophily_political$tweeter_ideology_final, 
    df_homophily_political$retweeter_ideology_final, 
    use='complete.obs')